﻿$(function () {
    $("#student-grid").datagrid({
        url: $.Api("Dri/Student/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        pageSize: 20, pagination: true,
        idField: "ID", toolbar: "#student-tools",
        remoteSort: false,
        //冻结
        frozenColumns:
        [[
            { field: "Num", title: "学员编号", width: 80, align: "center", sortable: true },
            { field: "AcceptingNum", title: "受理号", width: 80, align: "center", sortable: true },
            { field: "Name", title: "姓名", width: 58, align: "center", sortable: true }
        ]],
        //非冻结
        columns:
        [[
            { field: "Sex", title: "性别", width: 38, align: "center", sortable: true },
            //{ field: "CertificateType", title: "证件类型", width: 70, align: "center", sortable: true },
            { field: "Mobile", title: "手机", width: 82, align: "center", sortable: true },
            { field: "CertificateNum", title: "证件编号", width: 130, align: "center", sortable: true },
            { field: "ApplyType", title: "申请类型", width: 65, align: "center", sortable: true },
            { field: "SignUpDate", title: "报名时间", width: 80, align: "center", sortable: true },
            { field: "Registered", title: "户籍", width: 130, align: "center", sortable: true },
            {
                field: "IsResidencePermit", title: "代办居住证", width: 75, align: "center", sortable: true,
                formatter: function (v, r, i) { return $.GetYesOrNo(v); }
            },
            {
                field: "IsReturnCertificate", title: "已取回证件", width: 72, align: "center", sortable: true,
                formatter: function (v, r, i) { return $.GetYesOrNo(v); }
            },
            { field: "Recommender", title: "推荐人", width: 65, align: "center", sortable: true },
            {
                field: "IsArrears", title: "欠款", width: 40, align: "center", sortable: true,
                formatter: function (v, r, i) { return $.GetYesOrNo(v); }
            },
            { field: "ArrearsCondition", title: "欠款情况", width: 85, align: "center", sortable: true },
            //{ field: "Description", title: "备注", width: 85, align: "center", sortable: true },
            {
                field: "IsVip", title: "VIP", width: 40, align: "center", sortable: true,
                formatter: function (v, r, i) { return $.GetYesOrNo(v); }
            },
            {
                field: "IsVisible", title: "启用", width: 40, align: "center", sortable: true,
                formatter: function (v, r, i) { return $.GetYesOrNo(v); }
            }
        ]],
        rowStyler: function (index, row) {
            //如果备注信息已填写，则改背景色
            if (row.Description.length > 0) {
                return 'background-color:orange;';
            }
        },
        onDblClickRow: function (index, row) {
            Student.Update();
        }
    });

    $("#student-searchbox").searchbox({
        width: 340, prompt: "学员编号、受理号、姓名、证件号、手机、申请类型查询",
        searcher: function (value) {
            $("#student-grid").datagrid("clearChecked");
            $("#student-grid").datagrid("load", { search: value });
        }
    });
});

var Student = {
    //添加
    Insert: function () {
        Student.Mutual("添加", "dri/student/Edit.aspx", true);
    },
    //修改
    Update: function () {
        var row = $("#student-grid").datagrid("getSelected");
        if (row) {
            Student.Mutual("修改", "dri/student/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#student-grid").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Dri/Student/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Student.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作（isAdd表示是否新增，如果新增，则下拉框会默认选中第一个）
    Mutual: function (title, href, isAdd) {
        $.ModelDialog({
            title: title, href: href, width: 780,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    id: "#student-model",
                    url: $.Api("Dri/Student/Save"),
                    success: function (result) { Student.Reload(); }
                });
            },
            onLoad: function () { Student.FormInit(isAdd); }
        });
    },
    //弹窗初始化
    FormInit: function (isAdd) {
        $("#sex").combobox({
            width: 150, panelHeight: "auto",
            url: $.Api("Sys/Dictionary/List&tag=Gender"),
            valueField: 'text',
            textField: 'text',
            //如果是新增，则让下拉框默认选中第一个
            onLoadSuccess: function () {
                if (isAdd) {
                    var data = $('#sex').combobox('getData');
                    if (data.length > 0) {
                        $('#sex').combobox('select', data[0].text);
                    }
                }
            }
        });

        $("#certificateType").combobox({
            width: 150, panelHeight: "auto",
            url: $.Api("Sys/Dictionary/List&tag=CertificateType"),
            valueField: 'text',
            textField: 'text',
            //如果是新增，则让下拉框默认选中第一个
            onLoadSuccess: function () {
                if (isAdd) {
                    var data = $('#certificateType').combobox('getData');
                    if (data.length > 0) {
                        $('#certificateType').combobox('select', data[0].text);
                    }
                }
            }
        });

        $("#applyType").combobox({
            width: 150, panelHeight: "auto",
            url: $.Api("Sys/Dictionary/List&tag=ApplyType"),
            valueField: 'text',
            textField: 'text',
            //如果是新增，则让下拉框默认选中第一个
            onLoadSuccess: function () {
                if (isAdd) {
                    var data = $('#applyType').combobox('getData');
                    if (data.length > 0) {
                        $('#applyType').combobox('select', data[0].text);
                    }
                }
            }
        });
    },

    //启用
    Enable: function () {
        Student.EnableOrDisable(true);
    },
    //禁用
    Disable: function () {
        Student.EnableOrDisable(false);
    },
    //启用禁用
    EnableOrDisable: function (bool) {
        var row = $("#student-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Dri/Student/EnableOrDisable"),
                type: "post", data: { id: row.ID, bit: bool },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Student.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.DataGridReload("#student-grid");
    },

    //显示相片
    ShowPhoto: function (file, text, group) {
        if (!file) { text = "查看相片"; file = $("#student-photo").val(); }

        var w = 800, h = 480;
        var img = "<img style='display:block; margin:auto;' src='../userfiles/student/" + file + "' />";
        //如果是空，则赋空图片
        if (file.length == 0)
            img = "<img style='display:block; margin:auto;' src='/images/null.png' />";
        var box = "<div style='width:" + w + "px;height:" + h + "px;'>" + img + "</div>";

        $.ModelDialog({ title: text, iconCls: "icon-images", content: box });
        //$.ModelDialog({ title: text, width: w + 14, height: h + 14, iconCls: "icon-images", content: box });
    },
    //上传相片
    AddPhoto: function () {
        var w = 800, h = 480;
        KindEditor.editor({
            uploadJson: $.Api("Dri/Student/AddPhoto&w=" + w + "&h=" + h),
            formatUploadUrl: false
        }).loadPlugin("image", function () {
            this.plugin.imageDialog({
                showRemote: false,
                clickFn: function (url, title, width, height, border, align) {
                    $.EasyuiSlide("上传成功!"); this.hideDialog(); $("#student-photo").val(url);
                }
            });
        });
    },
    //清除相片
    DeletePhoto: function () {
        $("#student-photo").val("null.png"); $.EasyuiSlide("清除成功!");
    },

    //缴费
    Payment: function () {
        //$.EasyuiSlide("学员缴费");

        var row = $("#student-grid").datagrid("getSelected");
        if (row) {
            Student.PaymentMutual("缴费", "dri/student/Payment.aspx?studentID=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    PaymentMutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 300,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    id: "#payment-model",
                    url: $.Api("Dri/Student/SavePayment"),
                    success: function (result) { Student.Reload(); }
                });
            },
            onLoad: function () { Student.PaymentFormInit(); }
        });
    },

    PaymentFormInit: function () {
        $("#cost-type").combobox({
            width: 150, panelHeight: "auto",
            url: $.Api("Sys/Dictionary/List&tag=CostType"),
            valueField: 'text',
            textField: 'text',
            //如果是新增，则让下拉框默认选中第一个
            onLoadSuccess: function () {
                var data = $('#cost-type').combobox('getData');
                if (data.length > 0) {
                    $('#cost-type').combobox('select', data[0].text);
                }
            }
        });
    },

    //导出表格
    Export: function () {
        location.href = $.Api("Dri/Student/Export"); return false;

        //var search = $("#student-searchbox").val();
        //$.AjaxMask({
        //    url: $.Api("Dri/Student/Export"),
        //    type: "post", data: { search: search }
        //});
    },

    //练习
    Exercise: function () {
        var row = $("#student-grid").datagrid("getSelected");
        if (row) {
            Student.ExerciseMutual("安排练习", "dri/exercise/Edit.aspx?studentID=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    ExerciseMutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 500,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    id: "#exercise-model",
                    url: $.Api("Dri/Exercise/Save"),
                    success: function (result) { }
                });
            },
            onLoad: function () { Student.ExerciseFormInit(); }
        });
    },

    //弹窗初始化
    ExerciseFormInit: function () {
        $("#exerciseType").combobox({
            width: 150, panelHeight: "auto",
            url: $.Api("Sys/Dictionary/List&tag=ExerciseType"),
            valueField: 'text',
            textField: 'text'
        });

        $("#exercisePlace").combobox({
            width: 150, panelHeight: "auto",
            url: $.Api("Sys/Dictionary/List&tag=ExercisePlace"),
            valueField: 'text',
            textField: 'text'
        });

        $("#carID").combobox({
            width: 150, panelHeight: "auto",
            url: $.Api("Dri/Car/ComboBoxList"),
            valueField: 'id',
            textField: 'text'
        });

        $("#teacherID").combobox({
            width: 150, panelHeight: "auto",
            url: $.Api("Dri/Teacher/ComboBoxList"),
            valueField: 'id',
            textField: 'text'
        });
    }
};